package com.xoa.service.sms.impl;


import cn.jiguang.common.ClientConfig;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.*;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosAlert;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.huawei.push.javasdk.exception.HuaweiMesssagingException;
import com.huawei.push.javasdk.message.AndroidConfig;
import com.huawei.push.javasdk.message.BadgeNotification;
import com.huawei.push.javasdk.message.ClickAction;
import com.huawei.push.javasdk.messaging.*;
import com.tencent.xinge.MessageIOS;
import com.tencent.xinge.Style;
import com.tencent.xinge.XingeApp;
import com.xoa.dao.common.SysCodeMapper;
import com.xoa.dao.common.SysParaMapper;
import com.xoa.dao.voteItem.VoteItemMapper;
import com.xoa.dao.workflow.FlowProcessMapper;
import com.xoa.dao.notify.NotifyMapper;
import com.xoa.dao.sms.SmsBodyMapper;
import com.xoa.dao.sms.SmsMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.common.SysCode;
import com.xoa.model.common.SysPara;
import com.xoa.model.daiban.Daiban;
import com.xoa.model.notify.Notify;
import com.xoa.model.sms.Sms;
import com.xoa.model.sms.SmsBody;
import com.xoa.model.sms.SmsBodyExtend;
import com.xoa.model.sms.SmsExample;
import com.xoa.model.users.Users;
import com.xoa.model.voteItem.VoteItem;
import com.xoa.service.sms.SmsService;
import com.xoa.service.todoList.TodolistService;
import com.xoa.util.*;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.log.FileUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.ipUtil.MachineCode;
import com.xoa.util.page.PageParams;
import net.sf.json.JSONArray;
import org.apache.commons.io.IOUtils;
import org.apache.ibatis.jdbc.Null;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.json.JSONObject;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.text.MessageFormat;
import java.util.*;

import static cn.jpush.api.push.model.notification.PlatformNotification.ALERT;

/**
 * @作者：张航宁
 * @时间：2017/7/28
 * @介绍：实现类
 * @参数：
 */
@Service
public class SmsServiceImpl implements SmsService {

    @Resource
    private SmsMapper smsMapper;
    @Resource
    private SmsBodyMapper smsBodyMapper;
    @Resource
    private UsersMapper usersMapper;
    @Resource
    private SysCodeMapper sysCodeMapper;
    @Resource
    private HttpServletRequest request;
    @Resource
    private NotifyMapper notifyMapper;
    @Resource
    private VoteItemMapper voteItemMapper;
    @Autowired
    private FlowProcessMapper flowProcessMapper;
    @Autowired
    private SysParaMapper sysParaMapper;

    @Autowired
    private TodolistService todolistService;

    @Value("${xg_push_msg_android_key}")
    String androidKey;
    @Value("${xg_push_msg_android_sercetkey}")
    String androidSercetKey;
    @Value("${xg_push_msg_ios_key}")
    String iosKey;
    @Value("${xg_push_msg_ios_sercetkey}")
    String iosSercetKey;

    @Value("${xg_push_msg_ios_store_key}")
    String iosStoreKey;
    @Value("${xg_push_msg_ios_store_sercetkey}")
    String iosStoreSercetKey;

    @Value("${j_push_msg_key}")
    String JGKey;
    @Value("${j_push_msg_sercetkey}")
    String JGSercetKey;

    @Value("${j_push_msg_ios_store_key}")
    String JGIosStoreKey;
    @Value("${j_push_msg_ios_store_sercetkey}")
    String JGIosStoreSercetKey;

    @Value("${App_Id_Huawei_Key}")
    String AppIdKey;
    @Value("${SecretKey}")
    String SecretKey;
    @Value("${Huawei_badge_class}")
    String Huawei_badge_class;


    @Autowired
    ThreadPoolTaskExecutor threadPoolTaskExecutor;

    //用户在华为开发者联盟申请的appId和appSecret（会员中心->应用管理，点击应用名称的链接）
    private static String appSecret = "1ef4fa0d6b7cc6b06f4153579b478856";
    private static String appId = "100221479";

    /**
     * 获取认证Token的URL
     */
    private static String tokenUrl = "https://login.cloud.huawei.com/oauth2/v2/token";
    /**
     * 应用级消息下发API
     */

    private static String apiUrl = "https://api.push.hicloud.com/pushsend.do";
    //private static  String apiUrl = "https://push-api.cloud.huawei.com/v1/100221479/messages:send";
    private static String accessToken;//下发通知消息的认证Token
    private static long tokenExpiredTime;  //accessToken的过期时间

    /**
     * @作者：张航宁
     * @时间：2017/7/28
     * @介绍：保存提醒
     * @参数：
     */
    @Override
    public ToJson<SmsBody> saveSms(final SmsBody smsBody, final String toIds, final String remind, final String tuisong, final String title, final String context, final String sqlType) {
        // SysUserPushSerive sysUserPushSerive=new SysUserPushSerive();
        ToJson<SmsBody> json = new ToJson<SmsBody>();
        SmsBody smsBody1 = new SmsBody();
        Long curTime = System.currentTimeMillis();
        String jixiema = null;
        /* System.out.println("0=||============>++当前更新时间:"+curTime);*/
        SysPara para = sysParaMapper.querySysPara("PUSH_MNO");
        if (para != null && !StringUtils.checkNull(para.getParaValue())) {
            jixiema = para.getParaValue().substring(0, 12);
        } else {
            jixiema = MachineCode.get16CharMacs() == null ? "" : MachineCode.get16CharMacs().get(0);
        }

        //String jixiema = MachineCode.get16CharMacs().get(0);
        /*System.out.println("0=||===============》推送机器码："+jixiema);*/
        try {
            SysCode sysCode = sysCodeMapper.getSmsIsRemind(smsBody.getSmsType());
            if (Integer.parseInt(sysCode.getIsRemind()) == 1) {
                if (!StringUtils.checkNull(toIds)) {
                    if (remind.equals("1")) {//事务提醒标志
                        int a = 0;
                        if ("1".equals(smsBody.getEditFlag())) {
                            Map<String, Object> map = new HashMap<>();
                            map.put("smsType", smsBody.getSmsType());
                            map.put("remindUrl", smsBody.getRemindUrl());
                            smsBody1 = smsBodyMapper.querySmsBodyCon(map);
                            if (smsBody1 == null) {
                                if (smsBody.getBodyId() != null) {
                                    smsBody.setBodyId(null);
                                }
                                a = smsBodyMapper.insertSelective(smsBody);
                            } else {
                                smsBody.setBodyId(smsBody1.getBodyId());
                                String time = DateFormatUtils.formatDate(new Date());
                                smsBody.setSendTime(DateFormatUtils.getNowDateTime(time));
                                a = smsBodyMapper.updateByPrimaryKeySelective(smsBody);
                            }
                        } else {
                            a = smsBodyMapper.insertSelective(smsBody);
                        }
                        if (a > 0) {
                            String[] split = toIds.split(",");
                            List<String> list = new ArrayList<>();
                            for (int i = 0; i < split.length; i++) {
                                for (int j = i + 1; j < split.length; j++) {
                                    if (split[i] == split[j]) {
                                        j = ++i;
                                    }
                                }
                                list.add(split[i]);
                            }
                            List<Sms> smsList = new ArrayList<Sms>();
                            for (String toId : list) {
                                Sms sms = new Sms();
                                sms.setToId(toId);
                                if ("1".equals(smsBody.getEditFlag()) && smsBody1 != null) {
                                    sms.setBodyId(smsBody1.getBodyId());
                                } else {
                                    sms.setBodyId(smsBody.getBodyId());
                                }
                                sms.setRemindFlag("1");
                                sms.setDeleteFlag("0");
                                sms.setRemindTime((int) (System.currentTimeMillis() / 1000));
                                smsList.add(sms);
                                Map<String, Object> map = new HashMap<>();
                                map.put("toId", toId);
                                map.put("bodyId", sms.getBodyId());
                                Sms temp = smsMapper.selSmsByCon(map);
                                if (temp == null) {
                                    smsMapper.insertSelective(sms);
                                } else {
                                    temp.setRemindFlag("1");
                                    temp.setRemindTime((int) (System.currentTimeMillis() / 1000));
                                    smsMapper.updSmsById(temp);
                                }
                                json.setMsg("ok");
                            }
                            //cs通信息集成，发送事务提醒需向cs通app发送，，，代码只针对cs执行
                            try {
                                String sysPara = sysParaMapper.querySysPara("MYPROJECT").getParaValue();//防止有的产品没有myproject这个值
                                if(!smsBody.getSmsType().equals(1)&&!smsBody.getSmsType().equals(14)&&!StringUtils.checkNull(sysPara)&&sysPara.equals("powerchina")){
                                   // synPWDControl.insertSms(smsBody,list);
                                }
                            }catch (Exception e){

                            }
                        }
                    }
                }
            }

            if ("1".equals(tuisong)) {
                if (!StringUtils.checkNull(toIds)) {
                    String[] split = toIds.split(",");
                    List<String> list = new ArrayList<>();
                    for (int i = 0; i < split.length; i++) {
                        for (int j = i + 1; j < split.length; j++) {
                            if (split[i] == split[j]) {
                                j = ++i;
                            }
                        }
                        if (split[i] != null && !split[i].equals("")) {
                            list.add(split[i]);
                        }
                    }
                    String[] arrayResult = (String[]) list.toArray(new String[list.size()]);

                    //极光推送list
                    List<String> jgList = new ArrayList<>();
                    // 推送的真实用户id串
                    List<String> jgPushUserIds = new ArrayList<>();

                    //目标设备Token集合
                    com.alibaba.fastjson.JSONArray deviceTokens = new com.alibaba.fastjson.JSONArray();
                    //for (int i = 0,size = toIds.length(); i < size; i++) {
                    List<String> tokens = new ArrayList<String>();
                    for (int i1 = 0; i1 < arrayResult.length; i1++) {

                        //获取推送人的icqNo，即pushtoken
                        Users userAll = usersMapper.getUsersByuserId(arrayResult[i1]);
                        if(userAll!=null){
                            String token = userAll.getIcqNo();
                            //String token = null;
                            //  String token = toIds[i];
                            if (StringUtils.checkNull(token)) {
                                String account = jixiema + arrayResult[i1] + sqlType;
                                jgList.add(account);
                                jgPushUserIds.add(arrayResult[i1]);
                            } else {
                                tokens.add(token);
                                deviceTokens.add(token);
                               // Daiban db=todolistService.readTotal(userAll.getUserId(),sqlType,request,"");
                                Map<String, Object> smsCountmap = new HashMap<String, Object>();
                                // 查询未确认提醒的
                                smsCountmap.put("toId", arrayResult[i1]);
                                smsCountmap.put("remindFlag", "3");
                                Date date = new Date();
                                String s = DateFormatUtils.formatDate(date);
                                Integer nowDateTime = DateFormatUtils.getNowDateTime(s);
                                smsCountmap.put("sendTime", nowDateTime);
                                Integer smsBodyCountByMap = smsBodyMapper.getSmsBodyCountByMap(smsCountmap);
                                System.out.println("--=======>>>>" + smsBodyCountByMap);
                                this.testNotificationMessageSend(token, title, context,smsBodyCountByMap);

                            }
                        }
                    }
                    //华为旧版推送
                    if (tokens.size() > 0) {
                        if (StringUtils.checkNull(AppIdKey) && StringUtils.checkNull(SecretKey)) {
                            sendPushMessage(title, context, deviceTokens);
                        }
                    }

                    if (jgList.size() > 5) {

                        //极光推送数组(适配推送结构) 4CCC6A6247B8 891 1001
                        String[] jgArray = jgList.toArray(new String[jgList.size()]);
                        //极光推送通用版（ios+andriod）
                        JPushClient jpushClient = new JPushClient(JGSercetKey, JGKey, null, ClientConfig.getInstance());
                        PushPayload payload = buildPushObject_all_alias_alert(jgArray, title, context, 1);
                        PushResult result = jpushClient.sendPush(payload);
                       // System.out.println(result);
                    } else if (jgList.size() <= 5&&jgList.size()>0){ // 判断是否小于5人 如果小于5人 查询出所有人的待办进行设置推送
                        for (int k=0;k<jgPushUserIds.size();k++){
                            Map<String, Object> smsCountmap = new HashMap<String, Object>();
                            // 查询未确认提醒的
                            smsCountmap.put("toId", jgPushUserIds.get(k));
                            smsCountmap.put("remindFlag", "3");
                            Date date = new Date();
                            String s = DateFormatUtils.formatDate(date);
                            Integer nowDateTime = DateFormatUtils.getNowDateTime(s);
                            smsCountmap.put("sendTime", nowDateTime);
                            Integer smsBodyCountByMap = smsBodyMapper.getSmsBodyCountByMap(smsCountmap);

                            //极光推送数组(适配推送结构) 4CCC6A6247B8 891 1001
                            String[] jgArrayNew = new String[1];
                            jgArrayNew[0] = jgList.get(k);
                            //极光推送通用版（ios+andriod）
                            JPushClient jpushClient = new JPushClient(JGSercetKey, JGKey, null, ClientConfig.getInstance());
                            PushPayload payload = buildPushObject_all_alias_alert(jgArrayNew, title, context, smsBodyCountByMap);
                            PushResult result = jpushClient.sendPush(payload);
                        }
                    }


           /*         XingeApp pushAndroid = new XingeApp(Long.parseLong(androidKey), androidSercetKey);//安卓
                    XingeApp push = new XingeApp(Long.parseLong(iosKey), iosSercetKey);//ios企业版
                    XingeApp pushStore = new XingeApp(Long.parseLong(iosStoreKey), iosStoreSercetKey);//ios商业版

                    String[] acc = new String[arrayResult.length];
                    for (int ssi = 0; ssi < arrayResult.length; ssi++) {
                        String account = jixiema + arrayResult[ssi] + sqlType;
                        acc[ssi] = account;
                    }
                    //极光推送通用版（ios+andriod）
                    JPushClient jpushClient = new JPushClient(JGSercetKey, JGKey, null, ClientConfig.getInstance());
                    PushPayload payload = buildPushObject_all_alias_alert(acc, title, context);
                    PushResult result = jpushClient.sendPush(payload);

                    PushResult resultIos = null;
                    if (!JGKey.equals(JGIosStoreKey)) {
                        //极光推送ios商店版
                        JPushClient jpushClientios = new JPushClient(JGIosStoreSercetKey, JGIosStoreKey, null, ClientConfig.getInstance());
                        PushPayload payloadIos = buildPushObject_ios(acc, title, context);
                         resultIos = jpushClientios.sendPush(payloadIos);
                    }

                        if (!"200".equals(result.getResponseCode()) && !"200".equals(resultIos.getResponseCode())) {
                            for (int ssi = 0; ssi < arrayResult.length; ssi++) {
                                String account = jixiema + arrayResult[ssi] + sqlType;

                                ClickAction clickAction = new ClickAction();
                                clickAction.setActionType(ClickAction.TYPE_ACTIVITY);
                                clickAction.setActivity("123");
                                Style style = new Style(0, 1, 1, 1, 0, 1, 0, 1);
                                com.tencent.xinge.Message mess = new com.tencent.xinge.Message();
                                mess.setType(com.tencent.xinge.Message.TYPE_NOTIFICATION);
                                mess.setStyle(style);
                                mess.setTitle(title);
                                mess.setContent(context);
                                mess.setAction(clickAction);
                                JSONObject ret = pushAndroid.pushSingleAccount(0, account, mess);

                                JSONObject contentTest = new JSONObject();
                                JSONObject obj = new JSONObject();
                                JSONObject aps = new JSONObject();
                                contentTest.put("title", title);//标题
                                contentTest.put("body", context);//内容
                                aps.put("badge_type", 1);
                                aps.put("alert", contentTest);
                                aps.put("sound", "beep.wav");
                                obj.put("aps", aps);
                                MessageIOS messageIOS = new MessageIOS();
                                messageIOS.setRaw(obj.toString());
                                // messageIOS.setBadge(1);
                                JSONObject ret3 = push.pushSingleAccount(0, account, messageIOS, XingeApp.IOSENV_PROD);
                                JSONObject ret4 = pushStore.pushSingleAccount(0, account, messageIOS, XingeApp.IOSENV_PROD);

                            }
                        }*/

                }

            }
//            System.out.println("0=||============>++结束时间:"+(System.currentTimeMillis()-curTime));
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg("error");
        }

        return json;

    }

    /**
     * @作者：张航宁
     * @时间：2017/7/28
     * @介绍：提醒类型查询
     * @参数：queryType 1 未确认提醒 2已接收提醒 3已发送提醒
     */
    @Override
    public ToJson<SmsBody> getSmsBodies(HttpServletRequest request, Integer queryType, Integer page, Integer pageSize) {
        ToJson<SmsBody> json = new ToJson<SmsBody>();
        PageParams pageParams = new PageParams();
        pageParams.setPageSize(pageSize);
        pageParams.setPage(page);
        pageParams.setUseFlag(true);

        try {
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users sessionUser = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionCookie);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("page", pageParams);
            // 查询未确认提醒的
            if (queryType == 1) {
                map.put("toId", sessionUser.getUserId());
                map.put("remindFlag", "3");
            }
            // 查询所有接收到的
            if (queryType == 2) {
                map.put("toId", sessionUser.getUserId());
            }
            // 查询所有发送的
            if (queryType == 3) {
                map.put("fromId", sessionUser.getUserId());
            }
            Date date = new Date();
            String s = DateFormatUtils.formatDate(date);
            Integer nowDateTime = DateFormatUtils.getNowDateTime(s);
            map.put("sendTime", nowDateTime);
            List<SmsBody> smsBodyByMap = smsBodyMapper.getSmsBodyByMap(map);
            if (smsBodyByMap != null && smsBodyByMap.size() > 0) {
                for (SmsBody smsBody : smsBodyByMap) {
                    if (smsBody != null) {
                        if (!StringUtils.checkNull(smsBody.getFromId())) {
                            String usernameByUserId = usersMapper.getUsernameByUserId(smsBody.getFromId());
                            smsBody.setFromName(usernameByUserId);
                        }
                        if (!StringUtils.checkNull(smsBody.getToId())) {
                            String usernameByUserId = usersMapper.getUsernameByUserId(smsBody.getToId());
                            smsBody.setToName(usernameByUserId);
                        }
                        if (!StringUtils.checkNull(smsBody.getSmsType())) {
                            SysCode sms_remind = sysCodeMapper.getSingleCode("SMS_REMIND", smsBody.getSmsType());
                            if (sms_remind != null && sms_remind.getCodeName() != null) {
                                smsBody.setSmsTypeName(sms_remind.getCodeName());
                            } else {
                                smsBody.setSmsTypeName("类型不存在");
                            }

                        }
                    }
                }
            }
            map.remove("page");
            Integer smsBodyCountByMap = smsBodyMapper.getSmsBodyCountByMap(map);
            json.setTotleNum(smsBodyCountByMap);
            json.setObj(smsBodyByMap);
            json.setFlag(0);
            json.setMsg("ok");
        } catch (Exception e) {
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }

    /**
     * @作者：张航宁
     * @时间：2017/8/2
     * @介绍：更新删除状态（即页面中的删除操作）
     * @参数：request
     */
    @Override
    public ToJson<Sms> updateDeleteFlag(HttpServletRequest request, String deleteFlag, String bodyIds) {
        ToJson<Sms> json = new ToJson<Sms>();
        try {
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users sessionUser = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionCookie);
            String[] bodyIdsArr = null;
            if (!StringUtils.checkNull(bodyIds)) {
                bodyIdsArr = bodyIds.split(",");
            }
            smsMapper.updateDeleteFlag(sessionUser.getUserId(), deleteFlag, bodyIdsArr);
            json.setMsg("ok");
            json.setFlag(0);
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }
        return json;
    }

    /**
     * @作者：张航宁
     * @时间：2017/8/2
     * @介绍：更新阅读状态
     * @参数：remindFlag bodyIds
     */
    @Override
    public ToJson<Sms> updateRemindFlag(HttpServletRequest request, String remindFlag, String bodyIds) {
        ToJson<Sms> json = new ToJson<Sms>();
        try {
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users sessionUser = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionCookie);
            String[] bodyIdsArr = null;
            if (!StringUtils.checkNull(bodyIds)) {
                bodyIdsArr = bodyIds.split(",");
            }
            smsMapper.updateRemindFlag(sessionUser.getUserId(), remindFlag, bodyIdsArr != null && bodyIdsArr.length == 0 ? null : bodyIdsArr);
            json.setMsg("ok");
            json.setFlag(0);
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }
        return json;
    }

    /**
     * @作者：张航宁
     * @时间：2017/8/2
     * @介绍：删除所有已读
     * @参数：request
     */
    @Override
    public ToJson<Sms> deleteByRemind(HttpServletRequest request, String deleteType) {
        ToJson<Sms> json = new ToJson<Sms>();
        try {
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users sessionUser = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionCookie);
            if (sessionUser != null && !StringUtils.checkNull(sessionUser.getUserId())) {
                if ("1".equals(deleteType)) {
                    smsMapper.deleteByRemind(sessionUser.getUserId(), null);
                } else if ("2".equals(deleteType)) {
                    smsMapper.deleteByRemind(null, sessionUser.getUserId());
                }

            }
            json.setMsg("ok");
            json.setFlag(0);
        } catch (Exception e) {
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }

    /**
     * @作者：张航宁
     * @时间：2017/8/2
     * @介绍：删除所有收信人已删除的
     * @参数：request
     */
    @Override
    public ToJson<Sms> deleteByDelFlag(HttpServletRequest request) {
        ToJson<Sms> json = new ToJson<Sms>();
        try {
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users sessionUser = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionCookie);
            if (sessionUser != null && !StringUtils.checkNull(sessionUser.getUserId())) {
                smsMapper.deleteByDelFlag(sessionUser.getUserId());
            }
            json.setMsg("ok");
            json.setFlag(0);
        } catch (Exception e) {
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }

    /**
     * @作者：张航宁
     * @时间：2017/8/2
     * @介绍：设置已读
     * @参数：request bodyId
     */
    @Override
    public ToJson<Sms> setRead(HttpServletRequest request, Integer bodyId) {
        ToJson<Sms> json = new ToJson<Sms>();
        try {
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users sessionUser = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionCookie);
            if (sessionUser != null && !StringUtils.checkNull(sessionUser.getUserId())) {
                if (bodyId != null && bodyId != 0) {
                    smsMapper.setRead(sessionUser.getUserId(), bodyId);
                }
            }
            json.setMsg("ok");
            json.setFlag(0);
        } catch (Exception e) {
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }

    /**
     * @作者：张航宁
     * @时间：2017/8/3
     * @介绍：多条件查询
     * @参数：orderBy 排序字段 sortType 升序或降序 opeType 查询类型 1是查询 2是导出
     */
    @Override
    public ToJson<SmsBody> querySmsBody(HttpServletRequest request, HttpServletResponse response, String toIds, String fromIds, String smsType, String beginDate, String endDate, String content, String orderBy, String sortType, String opeType, Integer page,
                                        Integer pageSize, boolean useFlag) {

        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionCookie);
        ToJson<SmsBody> json = new ToJson<SmsBody>();
        Map<String, Object> map = new HashMap<String, Object>();
   /*     PageParams pageParams = new PageParams();
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        pageParams.setUseFlag(useFlag);*/
        if (page != null && pageSize != null) {
            page = (page - 1) * pageSize;
            map.put("page", page);
            map.put("pageSize", pageSize);
        } else {
            map.put("page", 0);
            map.put("pageSize", 100);
        }
        List<SmsBody> smsBodies = new ArrayList<SmsBody>();
        try {

            if (toIds != null) {
                String[] split = toIds.split(",");
                if (split.length > 0 && !"".equals(split[0])) {
                    map.put("toIds", split);
                } else {
                    map.put("toIds", null);
                }
                map.put("deleteFlag", 0);
                if (!"admin".equals(users.getByname())) {
                    String[] fromids = {users.getUserId()};
                    map.put("fromIds", fromids);
                    if (split.length <= 0 && !"".equals(split[0])) {
                        map.put("deleteFlag", 2);
                    }
                }
            }
            if (fromIds != null) {
                String[] split = fromIds.split(",");
                if (split.length > 0 && !"".equals(split[0])) {
                    map.put("fromIds", split);
                } else {
                    map.put("fromIds", null);
                }
                map.put("deleteFlag", 2);
                if (!"admin".equals(users.getByname())) {
                    String[] toids = {users.getUserId()};
                    map.put("toIds", toids);
                    if (split.length <= 0 && !"".equals(split[0])) {
                        map.put("deleteFlag", 0);
                    }
                }
            }


            if (!StringUtils.checkNull(beginDate)) {
                map.put("beginDate", DateFormat.getDateTime(beginDate));
            }
            if (!StringUtils.checkNull(endDate)) {
                map.put("endDate", DateFormat.getDateTime(endDate));
            } else {
                map.put("endDate", System.currentTimeMillis() / 1000);
            }
            if (!StringUtils.checkNull(content)) {
                map.put("content", content);
            }
            if (!StringUtils.checkNull(smsType)) {
                map.put("smsType", smsType);
            }
            if (!StringUtils.checkNull(orderBy)) {
                map.put("orderBy", orderBy);
            }
            if (!StringUtils.checkNull(sortType)) {
                map.put("sortType", sortType);
            }

            smsBodies = smsBodyMapper.querySmsBody(map);
            // 获取发送人或者收信人的名称
            if (smsBodies != null && smsBodies.size() > 0) {
                for (SmsBody smsBody : smsBodies) {
                    if (smsBody != null) {
                        if (!StringUtils.checkNull(smsBody.getFromId())) {
                            String usernameByUserId = usersMapper.getUsernameByUserId(smsBody.getFromId());
                            smsBody.setFromName(usernameByUserId);
                        }
                        if (!StringUtils.checkNull(smsBody.getToId())) {
                            String usernameByUserId = usersMapper.getUsernameByUserId(smsBody.getToId());
                            smsBody.setToName(usernameByUserId);
                        }
                        if (!StringUtils.checkNull(smsBody.getSmsType())) {
                            SysCode sms_remind = sysCodeMapper.getSingleCode("SMS_REMIND", smsBody.getSmsType());
                            if (sms_remind != null && sms_remind.getCodeName() != null) {
                                smsBody.setSmsTypeName(sms_remind.getCodeName());
                            } else {
                                smsBody.setSmsTypeName("类型不存在");
                            }

                        }
                    }
                }
            }
            if ("1".equals(opeType)) {
                json.setObj(smsBodies);
                json.setTotleNum(smsBodyMapper.querySmsBodyCount(map));
                json.setFlag(0);
                json.setMsg("ok");
            }
        } catch (Exception e) {
            e.printStackTrace();

            json.setMsg("err");
            json.setFlag(1);
        } finally {
            if ("2".equals(opeType)) {
                try {
                    HSSFWorkbook workbook1 = ExcelUtil.makeExcelHead("内部提醒", 4);
                    String[] secondTitles = {"类别", "发送人", "内容", "发送时间", "提醒"};
                    String[] beanProperty = {"smsTypeName", "fromName", "content", "sendTimeStr", "remindStr"};
                    if (toIds != null) {
                        secondTitles[1] = "收信人";
                        beanProperty[1] = "toName";

                    }
                    if (fromIds != null) {
                        secondTitles[1] = "发送人";
                        beanProperty[1] = "fromName";
                    }
                    HSSFWorkbook workbook2 = ExcelUtil.makeSecondHead(workbook1, secondTitles);
                    HSSFWorkbook workbook = ExcelUtil.exportExcelData(workbook2, smsBodies, beanProperty);
                    String filename = "内部提醒.xls";
                    filename = FileUtils.encodeDownloadFilename(filename, request.getHeader("user-agent"));
                    response.setContentType("application/vnd.ms-excel");
                    response.setHeader("content-disposition", "attachment;filename=" + filename);
                    ServletOutputStream out = response.getOutputStream();
                    workbook.write(out);
                    out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }
        return json;
    }

    @Override
    public void setRead(HttpServletRequest request, String url) {
        try {
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users sessionUser = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionCookie);
            if (sessionUser != null && !StringUtils.checkNull(sessionUser.getUserId())) {
                if (!StringUtils.checkNull(url)) {
                    smsMapper.setReadByUrl(sessionUser.getUserId(), url);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    @Override
    public void updatequerySmsByType(String type, String userId, String id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("type", type);
        map.put("userId", userId);
        map.put("id", id);
        try {
            List<Sms> smsList = smsMapper.querySmsByType(map);
            if (smsList != null && smsList.size() > 0) {
                for (Sms sms : smsList) {
                    smsMapper.setReadByUrl(sms.getToId(), sms.getUrl());
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void updateEndTimeList(HttpServletRequest request, String userId) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionCookie);
        if (StringUtils.checkNull(userId)) {
            userId = user.getUserId();
        }
        List<Notify> notifies = notifyMapper.selectNotifyEndTime(DateFormat.getCurrentTime2());
        List<VoteItem> voteItems = voteItemMapper.selectVoteEndTime(DateFormat.getCurrentTime2());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userId", user.getUserId());
        map.put("flag", "1");
        List<SmsBodyExtend> smsBodyExtendList = smsBodyMapper.SmsListByType(map);
        Iterator iter = smsBodyExtendList.iterator();
        List<SmsBodyExtend> list = new ArrayList<SmsBodyExtend>();//公告未看集合
        List<SmsBodyExtend> list1 = new ArrayList<SmsBodyExtend>();//投票未看集合
        while (iter.hasNext()) {
            SmsBodyExtend smsBodyExtend = (SmsBodyExtend) iter.next();
            switch (Integer.parseInt(smsBodyExtend.getSmsType())) {
                case 1://公告
                    String size2 = smsBodyExtend.getRemindUrl();
                    String[] aStrings2 = size2.split("\\?");
                    for (int i = 0; i < aStrings2.length; i++) {
                        if (aStrings2[i].contains("notifyId")) {
                            String[] s = aStrings2[i].split("=");
                            smsBodyExtend.setQid(Integer.parseInt(s[1]));
                            break;
                        }
                    }
                    list.add(smsBodyExtend);
                    break;
                case 11://投票
                    String size6 = smsBodyExtend.getRemindUrl();
                    String[] aStrings6 = size6.split("\\?");
                    for (int i = 0; i < aStrings6.length; i++) {
                        if (aStrings6[i].contains("resultId")) {
                            String[] s = aStrings6[i].split("=");
                            String[] s1 = s[i].split("&");
                            smsBodyExtend.setQid(Integer.parseInt(s1[0]));
                            break;
                        }
                    }
                    list1.add(smsBodyExtend);
                    break;
            }
        }
        if (notifies != null && notifies.size() > 0) {
            for (Notify notify : notifies) {
                if (list != null && list.size() > 0) {
                    for (int i = 0; i < list.size(); i++) {
                        if (notify.getNotifyId().equals(list.get(i).getQid())) {
                            updatequerySmsByType("1", user.getUserId(), String.valueOf(notify.getNotifyId()));
                        }
                    }
                } else {
                    break;
                }
            }
        }
        if (voteItems != null && voteItems.size() > 0) {
            for (VoteItem voteItems1 : voteItems) {
                if (list != null && list.size() > 0) {
                    for (int i = 0; i < list1.size(); i++) {
                        if (voteItems1.getVoteId().equals(list1.get(i).getQid())) {
                            String remingUrl = "/vote/manage/voteResult?resultId=" + voteItems1.getVoteId() + "&type=1";
                            updatequerySmsByType("11", user.getUserId(), remingUrl);
                        }
                    }
                } else {
                    break;
                }
            }
        }


    }

    @Override
    public void querySmsByTypeUpdateRunId(String type, String runId, Map<String, String> smsMap) {
        Map<String, Object> map = new HashMap<String, Object>();
        //String flowId=smsMap.get("flowId");
        //List<Integer> flowPrcsList=flowProcessMapper.selectShowPrcsId(Integer.parseInt(flowId));
        List<String> urlList = new ArrayList<>();
        String url = "%&runId=" + runId + "&%";
        urlList.add(url);
       /* for(Integer flowPrcs:flowPrcsList){
            String url = "/workflow/work/workform?opflag=1&flowId="+flowId+"&flowStep="+flowPrcs+"&runId="+runId+"%";
            urlList.add(url);
        }*/
        map.put("type", type);
        map.put("urlList", urlList);
        List<String> smsList = smsMapper.querySmsByTypeUpdateRunId(map);
        if (smsList != null && smsList.size() > 0) {
            String[] toBeStored = smsList.toArray(new String[smsList.size()]);
            smsMapper.updateSmsUpdateRunId("1", toBeStored);
        }


    }


    @Override
    public Integer getSmsId(HttpServletRequest request, String visitUrl) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users sessionUser = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionCookie);
        return smsMapper.getSmsId(sessionUser.getUserId(), visitUrl);
    }

    @Override
    public int getRemidFlagById(int id) {
        SmsExample smsExample = new SmsExample();
        SmsExample.Criteria criteria = smsExample.createCriteria();
        criteria.andSmsIdEqualTo(id);
        String remindflag = smsMapper.selectByExample(smsExample).get(0).getRemindFlag();
        return Integer.parseInt(remindflag);

    }

    /**
     * 将事务提醒未读改为已读
     *
     * @param request
     * @param bodyId
     */
    public void setSmsReadStatus(HttpServletRequest request, Integer bodyId) {
        if (bodyId != null) {
            smsMapper.setRead((String) request.getSession().getAttribute("userId"), bodyId);
        }
    }

    /**
     * 将事务提醒未读改为已读
     *
     * @param request
     * @param bodyId
     */
    public void setSmsReadByBodyId(HttpServletRequest request, Integer bodyId) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users sessionUser = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionCookie);
        SmsBody smsBody = smsBodyMapper.selectSmsBodyByBodyId(bodyId);
        if (smsBody != null) {
            smsMapper.setRead(sessionUser.getUserId(), smsBody.getBodyId());
        }

    }

    public void setSmsRead(String smsType, String remindUrl, Users users) {
        //查询出bodyId
        Map<String, Object> map = new HashMap<>();
        map.put("smsType", smsType);
        map.put("remindUrl", remindUrl);
        SmsBody smsBody = smsBodyMapper.querySmsBodyCon(map);
        //进行修改
        if (smsBody != null) {
            smsMapper.setRead(users.getUserId(), smsBody.getBodyId());
        }
    }

    public void setSmsFileRead(String bodyId, String smsType, String remindUrl, Users users) {
        //查询出bodyId
        Map<String, Object> map = new HashMap<>();
        map.put("bodyId", bodyId);
        map.put("smsType", smsType);
        map.put("remindUrl", remindUrl);
        SmsBody smsBody = smsBodyMapper.querySmsBodyCon(map);
        //进行修改
        if (smsBody != null) {
            smsMapper.setRead(users.getUserId(), smsBody.getBodyId());
        }
    }

    public void setSmsReadAll(String smsType, String remindUrl) {
        //查询出bodyId
        Map<String, Object> map = new HashMap<>();
        map.put("smsType", smsType);
        map.put("remindUrl", remindUrl);
        SmsBody smsBody = smsBodyMapper.querySmsBodyCon(map);
        if (smsBody != null) {
            smsMapper.updateSms(smsBody.getBodyId());
        }
    }

    @Override
    public void querySmsByTypeBathchUpdateRunId(String type, String[] runId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("type", type);
        map.put("url", runId);
        List<String> smsList = smsMapper.querySmsByTypeBatchUpdateRunId(map);
        if (smsList != null && smsList.size() > 0) {
            String[] toBeStored = smsList.toArray(new String[smsList.size()]);
            smsMapper.updateSmsBatchUpdateRunId("1", toBeStored);
        }
    }

    public Integer selectSmsId(HttpServletRequest request, String visitUrl) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users sessionUser = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionCookie);
        return smsMapper.selectSmsId(sessionUser.getUserId(), visitUrl);
    }

    @Override
    public Integer selectSmsId(String toUserId, String url) {
        return smsMapper.selectSmsId(toUserId, url);
    }

    /**
     * 快捷地构建推送对象：所有平台，所有设备，内容为 ALERT 的通知。
     *
     * @return
     */
    public static PushPayload buildPushObject_all_all_alert() {
        return PushPayload.alertAll(ALERT);
    }

    /**
     * 构建推送对象：所有平台，推送目标是别名为 "alias1"，通知内容为 ALERT。
     *
     * @return
     */
    public static PushPayload buildPushObject_all_alias_alert(String[] account, String alert, String message, Integer badge) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.alias(account))
                // .setNotification(Notification.alert(alert))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setAlert(alert)
                                .setTitle(message)
                                .build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(IosAlert.newBuilder()
                                        .setTitleAndBody(alert, "", message)
                                        .setActionLocKey("PLAY")
                                        .build())
                                .setBadge(badge)
                                .setSound("happy")
                                .addExtra("content-available",1)
                                .build())
                        .build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(true)
                        .build())
                .build();
    }

    /**
     * 构建推送对象：平台是 Android，目标是 tag 为 "tag1" 的设备，内容是 Android 通知 ALERT，并且标题为 TITLE。
     *
     * @return
     */
    public static PushPayload buildPushObject_android_tag_alertWithTitle() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.tag("tag1"))
                .setNotification(Notification.android(ALERT, TITLE, null))
                .build();
    }

    /**
     * 构建推送对象：平台是 iOS，推送内容同时包括通知与消息 - 通知信息是 ALERT，角标数字为 5，
     * 通知声音为 "happy"，并且附加字段 from = "JPush"；消息内容是 MSG_CONTENT。通知是 APNs 推送通道的，消息是 JPush 应用内消息通道的。
     * APNs 的推送环境是“生产”（如果不显式设置的话，Library 会默认指定为开发）
     *
     * @return
     */
    public static PushPayload buildPushObject_ios(String[] account, String alert, String message) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.alias(account))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(IosAlert.newBuilder()
                                        .setTitleAndBody(alert, "", message)
                                        .setActionLocKey("PLAY")
                                        .build())
                                .setBadge(1)
                                .setSound("happy")
                                .build())
                        .build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(true)
                        .build())
                .build();
    }


    /**
     * 构建推送对象：平台是 Andorid 与 iOS，推送目标是 （"tag1" 与 "tag2" 的并集）交（"alias1" 与 "alias2" 的并集），
     * 推送内容是 - 内容为 MSG_CONTENT 的消息，并且附加字段 from = JPush。
     *
     * @return
     */
    public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras(String account) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.newBuilder()
                        .addAudienceTarget(AudienceTarget.alias(account))
                        .build())
                .setMessage(Message.newBuilder()
                        .setMsgContent(MSG_CONTENT)
                        .addExtra("from", "JPush")
                        .build())
                .build();
    }

    /**
     * 构建推送对象：推送内容包含SMS信息
     */
   /* public static void testSendWithSMS() {
        JPushClient jpushClient = new JPushClient(androidJGSercetKey, androidJGKey );
        try {
            SMS sms = SMS.newBuilder()
                    .setDelayTime(1000)
                    .setTempID(2000)
                    .addPara("Test", 1)
                    .build();
            PushResult result = jpushClient.sendAndroidMessageWithAlias("Test SMS", "test sms", sms, "alias1");

        } catch (APIConnectionException e) {
          *//*  LOG.error("Connection error. Should retry later. ", e);*//*
        } catch (APIRequestException e) {
           *//* LOG.error("Error response from JPush server. Should review and fix it. ", e);
            LOG.info("HTTP Status: " + e.getStatus());
            LOG.info("Error Code: " + e.getErrorCode());
            LOG.info("Error Message: " + e.getErrorMessage());*//*
        }
    }
*/
    public static final String TITLE = "Test from API example";
    public static final String ALERT = "Test from API Example - alert";
    public static final String MSG_CONTENT = "Test from API Example - msgContent";
    public static final String REGISTRATION_ID = "0900e8d85ef";
    public static final String TAG = "tag_api";
    public static long sendCount = 0;
    private static long sendTotalTime = 0;


    /**
     * 华为推送
     */

    /**
     * 获取下发通知消息的认证Token
     */

    //获取下发通知消息的认证Token
    private static void refreshToken() throws IOException {
        String msgBody = MessageFormat.format(
                "grant_type=client_credentials&client_secret={0}&client_id={1}",
                URLEncoder.encode(appSecret, "UTF-8"), appId);
        String response = httpPost(tokenUrl, msgBody, 5000, 5000);
        com.alibaba.fastjson.JSONObject obj = com.alibaba.fastjson.JSONObject.parseObject(response);
        accessToken = obj.getString("access_token");
        tokenExpiredTime = System.currentTimeMillis() + obj.getLong("expires_in") - 5 * 60 * 1000;
    }


    //发送Push消息
    public void sendPushMessage(String title, String context, com.alibaba.fastjson.JSONArray deviceTokens) {
//        ToJson<SmsBody> json = new ToJson<SmsBody>();
        try {
            /**判断是否过期如果过期则重新获取token*/
            if (tokenExpiredTime <= System.currentTimeMillis()) {
                refreshToken();
            }

          /* //目标设备Token
           com.alibaba.fastjson.JSONArray  deviceTokens = new com.alibaba.fastjson.JSONArray();

           String[] split = toIds.split(",");
           List<String> list = new ArrayList<>();
           for (int i = 0; i < split.length; i++) {
               for (int j = i + 1; j < split.length; j++) {
                   if (split[i] == split[j]) {
                       j = ++i;
                   }
               }
               if (split[i] != null && !split[i].equals("")) {
                   list.add(split[i]);
               }
           }

           for(String id:list){
               //如果设备token不为空，把设备token添加到deviceTokens
               Users users = usersMapper.selectUserByUserId(id);
               if(users != null && users.getIcqNo() != null){
                   deviceTokens.add(users.getIcqNo());


               }
           }*/
            // deviceTokens.add("MWUBB18316204433300001479500CN01");


            /**发信息*/
            //仅通知栏消息需要设置标题和内容，透传消息key和value为用户自定义
            com.alibaba.fastjson.JSONObject body = new com.alibaba.fastjson.JSONObject();
            body.put("title", title);//发信人
            body.put("content", context);//消息内容体

            /**
             * 消息点击动作参数
             */
            com.alibaba.fastjson.JSONObject param = new com.alibaba.fastjson.JSONObject();
            param.put("appPkgName", "com.huawei.hms.hmsdemo");//定义需要打开的appPkgName

            /**
             * 消息点击动作
             * 定义消息的点击动作，type=1时为自定义动作，即intent。Type= 3为默认动作，打开app。
             */
            com.alibaba.fastjson.JSONObject action = new com.alibaba.fastjson.JSONObject();
            action.put("type", 3);
            action.put("param", param);//消息点击动作参数

            /**
             * push消息定义
             */
            com.alibaba.fastjson.JSONObject msg = new com.alibaba.fastjson.JSONObject();
            msg.put("type", 3);//3: 通知栏消息，异步透传消息请根据接口文档设置
            msg.put("action", action);//消息点击动作
            msg.put("body", body);//通知栏消息body内容

            /**
             * 扩展信息，含BI消息统计，特定展示风格，消息折叠。
             */
            com.alibaba.fastjson.JSONObject ext = new com.alibaba.fastjson.JSONObject();
            //设置消息标签，如果带了这个标签，会在回执中推送给CP用于检测某种类型消息的到达率和状态
            ext.put("biTag", "Trump");
            //自定义推送消息在通知栏的图标,value为一个公网可以访问的URL
            ext.put("icon", "http://8oa.cn/assets/main_img/huawei.jpg");

            /**
             * 华为push消息总结构体
             */
            com.alibaba.fastjson.JSONObject hps = new com.alibaba.fastjson.JSONObject();//华为PUSH消息总结构体
            hps.put("msg", msg);
            hps.put("ext", ext);

            /**
             * 放进总体结构，最高父级hps
             */
            com.alibaba.fastjson.JSONObject payload = new com.alibaba.fastjson.JSONObject();
            payload.put("hps", hps);

            /**
             * 请求发送接口
             */
            String postBody = MessageFormat.format(
                    "access_token={0}&nsp_svc={1}&nsp_ts={2}&device_token_list={3}&payload={4}",
                    URLEncoder.encode(accessToken, "UTF-8"),
                    URLEncoder.encode("openpush.message.api.send", "UTF-8"),
                    URLEncoder.encode(String.valueOf(System.currentTimeMillis() / 1000), "UTF-8"),
                    URLEncoder.encode(deviceTokens.toString(), "UTF-8"),
                    URLEncoder.encode(payload.toString(), "UTF-8"));

            String postUrl = apiUrl + "?nsp_ctx=" + URLEncoder.encode("{\"ver\":\"1\", \"appId\":\"" + appId + "\"}", "UTF-8");
            httpPost(postUrl, postBody, 5000, 5000);
        } catch (Exception e) {
            e.printStackTrace();

        }
    }


    /**
     * 推送
     *
     * @param httpUrl
     * @param data
     * @param connectTimeout
     * @param readTimeout
     * @return
     * @throws IOException
     */
    public static String httpPost(String httpUrl, String data, int connectTimeout, int readTimeout) throws IOException {
        OutputStream outPut = null;
        HttpURLConnection urlConnection = null;
        InputStream in = null;

        try {
            URL url = new URL(httpUrl);
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("POST");
            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);
            urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            urlConnection.setConnectTimeout(connectTimeout);
            urlConnection.setReadTimeout(readTimeout);
            urlConnection.connect();

            // POST data
            outPut = urlConnection.getOutputStream();
            outPut.write(data.getBytes("UTF-8"));
            outPut.flush();

            // read response
            if (urlConnection.getResponseCode() < 400) {
                in = urlConnection.getInputStream();
            } else {
                in = urlConnection.getErrorStream();
            }

            List<String> lines = IOUtils.readLines(in, urlConnection.getContentEncoding());
            StringBuffer strBuf = new StringBuffer();
            for (String line : lines) {
                strBuf.append(line);
            }
            //System.out.println(strBuf.toString());
            return strBuf.toString();
        } finally {
            IOUtils.closeQuietly(outPut);
            IOUtils.closeQuietly(in);
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }
    }

    public void testNotificationMessageSend(List<String> tokens, String title, String body) throws HuaweiMesssagingException {
        HuaweiCredential credential = HuaweiCredential.builder()
                .setAppId(AppIdKey)
                .setAppSecret(SecretKey)
                .build();

        HuaweiOption option = HuaweiOption.builder()
                .setCredential(credential)
                .build();
        HuaweiApp app = HuaweiApp.initializeApp(option);

        //String token = "1004103023061481981059500600CN01";
        com.huawei.push.javasdk.message.Notification notification = new com.huawei.push.javasdk.message.Notification(title, body);
        com.huawei.push.javasdk.message.ClickAction clickAction = ClickAction.builder()
                .setType(1)
                .setIntent("#Intent;compo=com.rvr/.Activity;S.W=U;end")
                .build();
        com.huawei.push.javasdk.message.AndroidNotification androidNotification = com.huawei.push.javasdk.message.AndroidNotification.builder()
                .setTitle(title)
                .setBody(body)
                .setClickAction(clickAction).setIcon("http://8oa.cn/assets/main_img/huawei.jpg")
                .build();

        AndroidConfig androidConfig = AndroidConfig.builder()
                .setNotification(androidNotification)
                .build();

        com.huawei.push.javasdk.message.Message message = com.huawei.push.javasdk.message.Message.builder()
                .setNotification(notification)
                .setAndroidConfig(androidConfig)
                .addAllToken(tokens)
                .build();

        SendResponce responce = HuaweiMessaging.getInstance(app).send(message, false);
        //app.clearInstancesForTest();
        Assert.assertEquals("Success", responce.getMsg());
    }

    public void testNotificationMessageSend(String tokens, String title, String body,Integer total) throws HuaweiMesssagingException {
        HuaweiCredential credential = HuaweiCredential.builder()
                .setAppId(AppIdKey)
                .setAppSecret(SecretKey)
                .build();

        HuaweiOption option = HuaweiOption.builder()
                .setCredential(credential)
                .build();
        HuaweiApp app = HuaweiApp.initializeApp(option);

        //String token = "1004103023061481981059500600CN01";
        com.huawei.push.javasdk.message.Notification notification = new com.huawei.push.javasdk.message.Notification(title, body);
        com.huawei.push.javasdk.message.ClickAction clickAction = ClickAction.builder()
                .setType(1)
                .setIntent("#Intent;compo=com.rvr/.Activity;S.W=U;end")
                .build();
        com.huawei.push.javasdk.message.AndroidNotification androidNotification = com.huawei.push.javasdk.message.AndroidNotification.builder()
                .setTitle(title)
                .setBody(body)
                .setClickAction(clickAction).setIcon("http://8oa.cn/assets/main_img/huawei.jpg")
                .setBadge(new BadgeNotification(total,Huawei_badge_class))
                .build();

        AndroidConfig androidConfig = AndroidConfig.builder()
                .setNotification(androidNotification)
                .build();

        com.huawei.push.javasdk.message.Message message = com.huawei.push.javasdk.message.Message.builder()
                .setNotification(notification)
                .setAndroidConfig(androidConfig)
                .addToken(tokens)
                .build();

        SendResponce responce = HuaweiMessaging.getInstance(app).send(message, false);
        //app.clearInstancesForTest();
        Assert.assertEquals("Success", responce.getMsg());
    }


}